import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class LoginController {
	private LoginView view = null;
	private LoginModel model = null;
	private JFrame frame;
	
	public LoginController(){
		frame = new JFrame();
		view = new LoginView();
		model = new LoginModel();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		doAction();
		frame.add(view);
		frame.setVisible(true);
		frame.setSize(300,180);
		frame.setResizable(false);
		
	}

	private void doAction() {
		// TODO Auto-generated method stub
		doLogin();
		doExit();
	}

	private void doExit() {
		// TODO Auto-generated method stub
		view.doExit(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				frame.dispose();
			}
		});
	}

	private void doLogin() {
		// TODO Auto-generated method stub
		view.doLogin(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				boolean validateUser = false;
				try {
					User user = view.getUser();
					validateUser = model.validateUsers(user);
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if(!validateUser){
					JOptionPane.showMessageDialog(frame,"Tên đăng nhập hoặc mật khẩu không đúng");
					return;
				}
				//new MainController();
				frame.dispose();
			}
		});
	}
	public static void main(String args[]){
		new LoginController();
	}
}
